package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName RoleController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-07-08
 * @Version V1.0
 **/
@RestController
@RequestMapping("/role")
@Slf4j

public class RoleController {
    @Reference
    RoleService roleService;

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("请求参数...." + queryPageBean);
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findPermission")
    public Result findPermission() {
        List<Permission> permissionList = roleService.findPermission();
        return new Result(true, MessageConst.PERMISSION_QUERY_SUCCEEDED, permissionList);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findMenu")
    public Result findMenu() {
        List<Menu> menuList = roleService.findMenu();
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
    }


    @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        Role role2 = roleService.findByNameAndKeyword(role);
        if (role2 != null) {
            throw new BusinessRuntimeException ("角色已存在");
        }
        roleService.add(role, permissionIds, menuIds);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findById")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer roleId) {
        List<Integer> list = roleService.findPermissionIdsByRoleId(roleId);
        return new Result(true, MessageConst.PERMISSION_QUERY_SUCCEEDED, list);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId) {
        List<Integer> list = roleService.findMenuIdsByRoleId(roleId);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, list);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleService.edit(role, permissionIds, menuIds);
        return new Result(true, MessageConst.EDIT_ROLE_SUCCESS);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
    @RequestMapping("delById")
    public Result delById(Integer id) {
        roleService.delById ( id );
        return new Result ( true, MessageConst.DELETE_ROLE_SUCCESS );
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug ( "查询所有角色信息" );
        List<Role> roles =roleService.findAll();
        log.debug ( "查询到的所有角色信息为："+roles.toString () );
        return new Result ( true, MessageConst.QUERY_ALL_ROLES_SUCCESSFULLY,roles );
    }
}
